package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public List<Menu> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public void add(Permission permission) {

        permissionDao.add(permission);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public Permission findById(Integer id) {

        return permissionDao.findById(id);
    }

    @Override
    public void edit(Permission permission) {

        permissionDao.edit(permission);
    }

    @Override
    public void delete(Integer id) {

        long count = permissionDao.findCountByPermissionId(id);

        if(count > 0){
            throw new RuntimeException("有关联,无法删除");
        }

        permissionDao.deleteById(id);
    }
}
